package com.demomaster.quickcompat.widget.dialog;

import static android.view.View.OVER_SCROLL_NEVER;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demomaster.quickcompat.R;
import com.demomaster.quickcompat.listener.OnSingleItemClickListener;
import com.demomaster.quickcompat.view.adapter.QuickRecyclerAdapter;
import com.demomaster.quickcompat.widget.button.QuickTextView;
import com.demomaster.quickcompat.widget.dialog.adapter.SheetAdapter;

import java.util.Arrays;
import java.util.List;

/**
 * @author squirrel桓
 * @date 2019/1/7.
 * description：
 */
public class QuickSheetDialog extends QuickDialog {
    private List<String> data;
    private int columnCount = 1;
    private ShowType showType = ShowType.List;
    private OnDialogActionListener onDialogActionListener;

    public QuickSheetDialog(Context context, Builder builder) {
        super(context,builder);
        data = builder.data;
        columnCount = builder.columnCount;
        showType = builder.showType;
        onDialogActionListener = builder.onDialogActionListener;
        init();
    }

    private void init() {
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        initData();
        //setCanSliding(false);
    }

    private void initData() {
        /*FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = gravity.value();*/
        RecyclerView recyclerView = findViewById(R.id.recycler);// new RecyclerView(getContext());
        recyclerView.setOverScrollMode(OVER_SCROLL_NEVER);
        switch (showType) {
            case List:
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                linearLayoutManager.setAutoMeasureEnabled(true);
                recyclerView.setLayoutManager(linearLayoutManager);
                break;
            case Grid:
                recyclerView.setLayoutManager(new GridLayoutManager(getContext(), columnCount));
                break;
        }

        SheetAdapter adapter = new SheetAdapter(getContext(), data);
        adapter.setOnItemClickListener(new OnSingleItemClickListener() {
            @Override
            public void onItemClickEvent(AdapterView<?> parent, View view, int position, long id) {
                if(onDialogActionListener!=null) {
                    onDialogActionListener.onItemClick(QuickSheetDialog.this, position, data);
                }
            }
        });

        recyclerView.setAdapter(adapter);
       /* RelativeLayout relativeLayout = new RelativeLayout(getContext());
        relativeLayout.addView(recyclerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setContentView(relativeLayout, layoutParams);*/
    }

    public void setOnDialogActionListener(OnDialogActionListener onDialogActionListener) {
        this.onDialogActionListener = onDialogActionListener;
    }
    
    public enum ShowType {
        List, Grid
    }

    public static class Builder extends QuickDialog.Builder<Builder>{
        private List<String> data;
        private Context context;
        private int columnCount = 1;
        private ShowType showType = ShowType.List;
        private OnDialogActionListener onDialogActionListener;

        public Builder(Context context) {
            super(context);
            this.context = context;
            setContentView(R.layout.quick_dialog_share_default);
        }

        /*@Override
        public <T extends Builder> T setContentView(int layoutResID) {
            this.layoutResID = layoutResID;
            return (T) this;
        }*/
        public Builder setData(List<String> data) {
            this.data = data;
            return this;
        }

        public Builder setData(String[] data) {
            this.data = Arrays.asList(data);
            return this;
        }

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setShowType(ShowType showType) {
            this.showType = showType;
            return this;
        }

        public Builder setColumnCount(int columnCount) {
            this.columnCount = columnCount;
            return this;
        }

        public Builder setOnDialogActionListener(OnDialogActionListener onDialogActionListener) {
            this.onDialogActionListener = onDialogActionListener;
            return this;
        }
        @Override
        public QuickSheetDialog create() {
            return new QuickSheetDialog(context, this);
        }
    }

    public interface OnDialogActionListener {
        void onItemClick(QuickSheetDialog dialog, int position, List<String> data);
    }
//
//    public static class SheetAdapter extends QuickRecyclerAdapter<SheetAdapter.VHItem> {
//        private List data;
//        public Context context;
//        public SheetAdapter(Context context, List data) {
//            this.context = context;
//            this.data = data;
//        }
//
//        @NonNull
//        @Override
//        public VHItem onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//            View view = LayoutInflater.from(context).inflate(R.layout.item_quick_sheet_dialog, viewGroup, false);
//            return new VHItem(view);
//        }
//
//        @Override
//        public void onBindViewHolder(@NonNull final VHItem vhItem, int i) {
//            vhItem.onbind(i);
//        }
//
//        @Override
//        public int getItemCount() {
//            if(this.data!=null){
//                return this.data.size();
//            }
//            return 0;
//        }
//
//        public class VHItem extends RecyclerView.ViewHolder {
//            QuickTextView textView;
//
//            public VHItem(@NonNull View itemView) {
//                super(itemView);
//                textView = itemView.findViewById(R.id.tv_text);
//                if (Build.VERSION.SDK_INT >= 19) {
//                    textView.setBackgroundResource(R.drawable.ripple_bg);
//                }
//            }
//
//            public void onbind(int position) {
//                textView.setText(data.get(position) + "");
//            }
//        }
//    }

}
